package org.linlinjava.litemall.wx.web;

import org.apache.commons.beanutils.PropertyUtilsBean;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallBookReview;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxBookReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/wx/bookReview")
public class WxBookReviewController {
    @Autowired
    private WxBookReviewService wxBookReviewService;

    /**
     * 创建书评
     * @param userId
     * @param bookReview
     * @return
     */
    @PostMapping("/create")
    public Object create(@LoginUser Integer userId, @RequestBody LitemallBookReview bookReview) {
        return wxBookReviewService.create(userId,bookReview);
    }

    /**
     * 删除书评
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody Integer id) {
        return ResponseUtil.ok(wxBookReviewService.delete(id));
    }

    /**
     * 获取书评详情
     * @param bookReviewId
     * @return
     */
    @GetMapping("/getDetail")
    public Object getDetail(@RequestParam Integer bookReviewId) {
        return wxBookReviewService.getDetail(bookReviewId);
    }

    /**
     * 根据不同状态（0：全部:1：待审核，2：通过，3：未通过）查询list
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/queryList")
    public Object queryList(Integer status,
                            @RequestParam(required = false,defaultValue = "1") Integer page,
                            @RequestParam(required = false,defaultValue = "10") Integer limit,
                            @Sort @RequestParam(defaultValue = "add_time") String sort,
                            @Order @RequestParam(defaultValue = "desc") String order) {
        return ResponseUtil.okList(wxBookReviewService.queryList(status, page, limit, sort, order));
    }

    /**
     * 根据话题查询书评
     * @param topicId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/queryByTopic")
    public Object queryByTopic(Integer topicId,
                               @RequestParam(required = false,defaultValue = "1")Integer page,
                               @RequestParam(required = false,defaultValue = "10")Integer limit,
                               @RequestParam(required = false,defaultValue = "add_time") String sort,
                               @RequestParam(required = false,defaultValue = "desc") String order) {
        return ResponseUtil.okList(wxBookReviewService.queryByTopic(topicId, page, limit, sort, order));
    }

    /**
     * 查询某本书下面的书评
     * @param goodsId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/queryByGoodsId")
    public Object queryByGoodsId(@RequestParam Integer goodsId,
                                 @RequestParam(required = false,defaultValue = "1")Integer page,
                                 @RequestParam(required = false,defaultValue = "10")Integer limit,
                                 @RequestParam(required = false,defaultValue = "add_time") String sort,
                                 @RequestParam(required = false,defaultValue = "desc") String order) {
        return ResponseUtil.okList(wxBookReviewService.queryByGoodsId(goodsId, page, limit, sort, order));
    }

    /**
     * 查看我的书评
     * @param userId
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/myBookReview")
    public Object queryByUserId(@LoginUser Integer userId,
                                @RequestParam(required = false,defaultValue = "1")Integer page,
                                @RequestParam(required = false,defaultValue = "10")Integer limit,
                                @RequestParam(defaultValue = "add_time") String sort,
                                @RequestParam(defaultValue = "desc") String order) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.okList(wxBookReviewService.queryListByUserId(userId, page, limit, sort, order));
    }
}
